package com.letv.util;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterOutputStream;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.letv.conf.ConfigFactory;

public class CommonUtil {

    public static final String COMMON_JOIN_CHARACTER = ",";

    private static final String TEXT_MESSAGE_URL = ConfigFactory.getString("textMessage.url");

    private static final String TEXT_MESSAGE_TOKEN = ConfigFactory.getString("textMessage.token");

    private static final String TEXT_MESSAGE_MOBILE = ConfigFactory.getString("textMessage.mobile");

    private final static Logger LOGGER = LoggerFactory.getLogger(CommonUtil.class);

    /**
     * zlib+base64压缩
     * @param data String
     * @return String, 为空或出错则返回null
     */
    public static String compressZlibBase64(String data) {
        if(StringUtils.isBlank(data)) {
            return null;
        }
        String result = null;
        ByteArrayOutputStream byteArrayOutputStream = null;
        try {
            byteArrayOutputStream = new ByteArrayOutputStream();
            DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream);
            deflaterOutputStream.write(data.getBytes());
            deflaterOutputStream.close();
            result = new String(Base64.encodeBase64(byteArrayOutputStream.toByteArray()));
        } catch (IOException e) {
            LOGGER.error("method CommonUtil.compressZlibBase64 catch error", e);
            result = null;
        } finally {
            try {
                byteArrayOutputStream.close();
            } catch (IOException ee) {
                LOGGER.error("method CommonUtil.compressZlibBase64 fail to close", ee);
            }
        }
        return result;
    }

    /**
     * zlib+base64解压
     * @param data String
     * @return String, 为空或出错则返回null
     */
    public static String decompressZlibBase64(String data) {
        if(StringUtils.isBlank(data)) {
            return null;
        }
        String result = null;
        ByteArrayOutputStream byteArrayOutputStream = null;
        InflaterOutputStream inflaterOutputStream = null;
        try {
            byteArrayOutputStream = new ByteArrayOutputStream();
            inflaterOutputStream = new InflaterOutputStream(byteArrayOutputStream);
            inflaterOutputStream.write(Base64.decodeBase64(data.getBytes()));
            result = new String(byteArrayOutputStream.toByteArray());
        } catch (Exception e) {
            LOGGER.error("method CommonUtil.decompressZlibBase64 catch error", e);
            result = null;
        } finally {
            try {
                inflaterOutputStream.close();
                byteArrayOutputStream.close();
            } catch (IOException ee) {
                LOGGER.error("method CommonUtil.decompressZlibBase64 fail to close", ee);
            }
        }
        return result;
    }

    /**
     * 发送http get请求
     * @param url String, 形如http://api.911.letv.cn/alarm/sms
     * @param param String, 形如p1=啊&p2=1
     * @return String, 为空或出错则返回null
     */
    public static String sendHttpGet(String url, String param) {
        String result = "";
        BufferedReader bufferedReader = null;
        try {
            URLConnection uRLConnection = new URL(url + "?" + param).openConnection();
            uRLConnection.setRequestProperty("accept", "*/*");
            uRLConnection.setRequestProperty("connection", "Keep-Alive");
            uRLConnection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            uRLConnection.connect();
            bufferedReader = new BufferedReader(new InputStreamReader(uRLConnection.getInputStream()));
            String line = null;
            StringBuffer resultBuffer = new StringBuffer("");
            while ((line = bufferedReader.readLine()) != null) resultBuffer.append(line);
            result = resultBuffer.toString();
        } catch(Exception e) {
            LOGGER.error("method CommonUtil.sendHttpGet catch error", e);
        } finally {
            try {
                if(null != bufferedReader) bufferedReader.close();
            } catch (Exception ef) {
                LOGGER.error("method CommonUtil.sendHttpGet fail to close", ef);
            }
        }
        return result;
    }

    /**
     * 发短信
     * @param content String, 短信内容
     */
    public static void sendTextMessage(String content) {
        try {
            CommonUtil.sendHttpGet(CommonUtil.TEXT_MESSAGE_URL
                    , "token=" + CommonUtil.TEXT_MESSAGE_TOKEN 
                    + "&msg=" + content 
                    + "&mobile=" + CommonUtil.TEXT_MESSAGE_MOBILE);
        } catch(Exception e) {
            LOGGER.error("method CommonUtil.sendTextMessage catch error", e);
        }
    }
}
